package com.tablefood.dao.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.tablefood.dao.ProductDao;
import com.tablefood.domain.ShopProduct;
import com.tablefood.domain.ShopUser;

public class ProductDaoImpl implements ProductDao {

	
	@Resource
	private SessionFactory sessionFactory; 
	
	@Override
	//添加商品
	public boolean addProduct(ShopProduct product) {
		System.out.println("存入商品结束");
		try{
		sessionFactory.getCurrentSession().save(product); 
		}catch(Exception e){
			e.printStackTrace(); 
			System.out.println("存入商品失败");
			return false;
		}
		System.out.println("存入商品成功");
		return true;
	}
 
	//删除商品-更改商品销售状态
	@Override
	public boolean delProducts(Long productid) {	
		ShopProduct product = (ShopProduct)sessionFactory.getCurrentSession().load(ShopProduct.class, productid);
		product.setState("0");
		sessionFactory.getCurrentSession().update(product);
		return true;
	}

	
	/*
	 * 根据productid查询某件商品
	 * */
	@Override
	public ShopProduct searchProducts(Long productid) {
		
		String sql = "from ShopProduct as product where product.productid=?";
		System.out.println("查找某件商品语句："+sql);
		Query query = sessionFactory.getCurrentSession().createQuery(sql); 
		query.setLong(0,productid);
		System.out.println("---查询结果----");
		System.out.println("是否有结果query.list().size()："+query.list().size()); 
		if(query.list().size()!=0){
			System.out.println("返回result");
			List<ShopProduct> result = query.list(); 
			ShopProduct product = result.get(0);
			return product;
			
		}else{
			System.out.println("没有数据");
			return null ;
		}
		
	}
	
    /*
     * 根据种类名categoryName查询特定商品
     * */
	@Override
	public List searchProducts(String categoryName, int pageIndex, int pageSize) {
		String Hql = "from " +
				"       ShopProduct as product " +
			         " where " +
			            " product.shopCategory.title=?";
		System.out.println("查找某件商品语句："+Hql);
		Query query = sessionFactory.getCurrentSession().createQuery(Hql); 
		query.setString(0,categoryName).setFirstResult((pageIndex-1)*pageSize).setMaxResults(pageSize);
		System.out.println("---查询结果----");
		System.out.println("是否有结果query.list().size()："+query.list().size()); 
		if(query.list().size()!=0){
			System.out.println("返回result");
			List result = query.list(); 
			return result;
			
		}else{
			System.out.println("没有数据");
			return null ;
		}
	}

	/*根据种类名categoryName查询相应商品的总量*/
	@Override
	public int getProductsCount(String categoryName) {
	 
		int count=0;
		String Hql="select count(*)" +
			       "from " +
		              " ShopProduct as product " +
                   " where " +
                   " product.shopCategory.title=?";
		Query query=sessionFactory.getCurrentSession().createQuery(Hql);
		query.setString(0, categoryName);
		if(query.list().size()!=0){
			count=((Long)(query.iterate().next())).intValue();
		}
			return count;
		  
		
	}

	//更新商品信息
	@Override
	public boolean updateProduct(ShopProduct product) {
		try{
		sessionFactory.getCurrentSession().saveOrUpdate(product);
		}catch(Exception e){
			e.printStackTrace(); 
			System.out.println("更新失败");
			return false;
		}
		System.out.println("更新成功");
		return true;
	}

	//分页查询
	@Override
	public List<ShopProduct> queryForPage(String hql, int offset, int length) {
		 
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setFirstResult(offset);
		query.setMaxResults(length);
		List<ShopProduct> list = query.list();
	 
		return list;
	}
	
	//统计有多少
	@Override
	public int getAllRowCount(String hql) {
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		return query.list().size();
	}

	@Override
	public boolean checkProductTitle(String title) {
		String hql = "FROM ShopProduct AS u WHERE u.title = ?"; 
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, title);  
		List<ShopProduct> product = query.list();
		if (product.size() > 0) {
			return true;
		}else{
			return false;
		}
	}

	@Override
	public int getAllRowCount(String hql, String search) {
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString("param", "%" + search + "%");
		return query.list().size();
	 
	}

	@Override
	public List<ShopProduct> queryForPage(String hql, String search,
			int offset, int length) {
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString("param", "%" + search + "%");
		query.setFirstResult(offset);
		query.setMaxResults(length);
		List<ShopProduct> list = query.list();
		return list;
	}

	 
	
}
